# -*- coding: UTF-8 -*-
from sympy import *
import random
from multiprocessing import Queue
import time, threading
from fractions import Fraction
import sys, os


class Pump:
    '''
    def __init__(self, x_coordinate, y_coordinate, z_coordinate, voltage, electric_current, power):
        self.x_coordinate = x_coordinate
        self.y_coordinate = y_coordinate
        self.z_coordinate = z_coordinate
        self.voltage = voltage
        self.electric_current = electric_current
        self.power = power
    '''
    """
     rotate_speed 电机转速
     electric_tension 电压 380V
     electric_current 电流
     P 电机电机数
     V 水泵排量 0.00814 立方米每分钟
     rated_power 额定功率
     
     功率因数 cosφ 0.86 
     效率通过拟合的效率曲线计算
     """

    def __init__(self, x_coordinate, y_coordinate, z_coordinate, frequency, rotate_speed, P, V, electric_tension,
                 rated_power, electric_current):
        self.x_coordinate = x_coordinate
        self.y_coordinate = y_coordinate
        self.z_coordinate = z_coordinate
        self.frequency = frequency
        self.rotate_speed = rotate_speed
        self.P = P
        self.V = V
        self.electric_tension = electric_tension
        self.rated_power = rated_power
        self.electric_current = electric_current

    # 自变量 :x
    # 因变量 :y
    x = Symbol('x')
    y = Symbol('y')
    z = Symbol('z')
    # 函数具体表达式待定，以下为自定义函数
    vibrate_func = sin(x) - y
    axleTemperatrue_func = 2 * x - y  # x，y
    # 电机转速和功率是   功率正比于转速的三次
    rotate_speed_func = 3 * x / y - z  # x,y,z 电压，电流,转速
    # 转速 流量  功率
    HQ_func = 152.74553105333754 + 0.02930069686386001 * x - 5.9441066191347876e-05 * x * x - y
    # 电机效率曲线
    yita_func = 1.179e-07 * x * x * x - 0.0003238 * x * x + 0.2842 * x + 0.2069 - y

    def produceVibrate(self, x):
        global vibrate_func
        vibrate_x, vibrate_y, vibrate_z = solve(vibrate_func, x)

    def produceAxleTem(self, x):
        global axleTemperatrue_func
        axleTemperatrue = solve(axleTemperatrue_func, x)

    # 给变频留接口
    def produceFrequency(self, frequency: Queue):
        while True:
            self.frequency = Fraction(1, 60)
            # 将频率写入队列
            frequency.put(self.frequency)
            print('frequency is %f ' % self.frequency)
            # mei每秒产生一个数据
            time.sleep(1)

    def produceFlow(self, frequency: Queue, Q1: Queue, q2: Queue, q3: Queue):
        while True:
            self.frequency = frequency.get(True)
            self.rotate_speed = (60 * self.frequency) / self.P
            print('rotate speed %f ' % self.rotate_speed)
            # 根据中开泵给出的数据，计算中开泵的排量 V : 0.00814 立方米   Q * t = n * V
            # Q 流量 ；t 时间   ;  n  转速 ;   V 水泵排量
            flow = self.rotate_speed * self.V
            # q1用于 计算扬程
            Q1.put(flow)
            print('this is flow : %f' % flow)
            # q2用于计算抽水池
            q2.put(flow)
            # q3用于计算蓄水池
            q3.put(flow)


    # 变频器的输出频率与输出电压基本近似于平方
    # 设额定电压380V，额定频率60HZ
    def computePower(self, flow: Queue):
        global yita_func
        now_power = solve(yita_func, flow.get(True))
        print('power is %f ' % now_power)
        yita = now_power / self.rated_power
        self.electric_current = self.rated_power / self.electric_tension / sqrt(3) / 0.86 / yita
        print('current is %f' % self.electric_current)
        now_tension = now_power / self.electric_current
        print('tension is %f' % now_tension)

    # 扬程
    def produceRaise(self, q1: Queue, q2: Queue):
        # global HQ_func,x
        while True:
            self.x = q1.get(True)
            H_num = 152.74553105333754 + 0.02930069686386001 * self.x - 5.9441066191347876e-05 * self.x * self.x
            q2.put(H_num)
            print('This is yangcheng : %f' % H_num)
